#include "../inc/chat_interface.h"

bool exit_flag = false;


// 聊天界面控件显示
void chat_inf_demo()
{
    // 创建主画布
    lv_obj_t *main_windows = lv_obj_create(NULL);

    chat_background(main_windows);

    client_func_btn(main_windows);

    break_btn(main_windows);

    show_chatlist(main_windows);

    title_label(main_windows);

    // login_interface(main_windows);

    // 加载到屏幕
    lv_scr_load(main_windows);

    return;
}

// 文本输入框事件回调函数
void login_ta_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED || code == LV_EVENT_FOCUSED)
    {
        // 设置键盘与文本输入框关联
        if (login_kb != NULL)
            lv_keyboard_set_textarea(login_kb, ta);
    }

    else if (code == LV_EVENT_READY)
    {
        printf("%s\n", lv_textarea_get_text(ta));
        lv_textarea_set_text(pwd_ta, "");            // 清空文本框
    }
}

// 聊天窗口背景图
void chat_background(lv_obj_t *main_windows)
{

    lv_obj_t *img_obj = lv_img_create(main_windows);

    // 图片路径
    lv_img_set_src(img_obj, "S:/mdata/project_2/res/img/Chat_3.png");

    // 设置图片位置
    lv_obj_set_pos(img_obj, 0, 0);

    return;
}

// 聊天列表
void show_chatlist(lv_obj_t *main_windows)
{
    LV_FONT_DECLARE(lv_font_HeiTi_Bold_18_cjk);

    lv_obj_t *list_obj = lv_list_create(main_windows);
    lv_obj_set_size(list_obj, 181, 49);
    lv_obj_set_style_radius(list_obj, 0, LV_PART_MAIN);
    lv_obj_set_pos(list_obj, 76, 59);

    lv_obj_t *list_btn1 = lv_list_add_btn(list_obj, LV_SYMBOL_LIST, "  聊天列表");
    lv_obj_add_event_cb(list_btn1, online_list_event_cb, LV_EVENT_CLICKED, "online");

    lv_obj_set_style_text_font(list_btn1, &lv_font_HeiTi_Bold_18_cjk, 0);
    lv_obj_set_style_border_color(list_btn1, lv_color_hex(0x000000), LV_PART_MAIN);
}

void online_list_event_cb(lv_event_t *e)
{
    // 获取事件的用户数据
    const char *btn_text = (const char *)lv_event_get_user_data(e);
    printf("按下 %s\n", btn_text);
    if (connect_flag == 1) // 已连接服务器
    {
        if (mode != 0)
        {
            prompt_msgbox("请先关闭当前聊天窗口!");
            return;
        }
        if (strcmp(btn_text, "online") == 0)
        {
            mode = 4;
        }
    }
    else // 未连接服务器
    {
        if (strcmp(btn_text, "online") == 0)
        {
            prompt_msgbox("未连接服务器，请先连接");
        }
    }
}

// 客户端主要功能按钮
void client_func_btn(lv_obj_t *main_windows)
{
    LV_FONT_DECLARE(lv_font_HeiTi_Light_16_cjk);

    // 客户端连接服务器按钮
    lv_obj_t *btn_obj = lv_btn_create(main_windows);
    lv_obj_t *label_connect = lv_label_create(btn_obj);
    lv_obj_set_pos(btn_obj, 2, 80); // 位置
    lv_label_set_text(label_connect, "     连接       服务器");
    lv_obj_set_style_text_font(label_connect, &lv_font_HeiTi_Light_16_cjk, 0);
    lv_obj_add_event_cb(btn_obj, client_btn_cb, LV_EVENT_CLICKED, "connect");
    set_button_style(btn_obj, label_connect);

    // 天气获取按钮
    lv_obj_t *btn_weather_chat = lv_btn_create(main_windows);
    lv_obj_t *label_weather = lv_label_create(btn_weather_chat);
    lv_obj_set_pos(btn_weather_chat, 2, 160); // 位置
    lv_label_set_text(label_weather, " 获取天气");
    lv_obj_set_style_text_font(label_weather, &lv_font_HeiTi_Light_16_cjk, 0);
    lv_obj_add_event_cb(btn_weather_chat, client_btn_cb, LV_EVENT_CLICKED, "weather");
    set_button_style(btn_weather_chat, label_weather);
}

// 定义一个设置按钮样式的函数
void set_button_style(lv_obj_t *btn_obj, lv_obj_t *label_obj)
{
    lv_obj_set_size(btn_obj, 73, 50); // 大小
    lv_obj_set_style_bg_color(btn_obj, lv_color_hex(0x696969), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(btn_obj, 0, LV_PART_MAIN); // 透明度
    lv_obj_set_style_radius(btn_obj, 0, LV_PART_MAIN); // 圆角

    lv_label_set_long_mode(label_obj, LV_LABEL_LONG_WRAP); // 设置标签的文本溢出模式为自动换行
    lv_obj_set_width(label_obj, 70);
    lv_obj_set_style_text_color(label_obj, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_center(label_obj);
}

// 事件处理回调函数
void client_btn_cb(lv_event_t *e)
{
    // 获取事件的用户数据
    const char *btn_text = (const char *)lv_event_get_user_data(e);
    printf("按下 %s\n", btn_text);
    if (connect_flag == 1) // 已连接服务器
    {
        if (mode != 0)
        {
            prompt_msgbox("请先关闭当前聊天窗口!");
            return;
        }
        if (strcmp(btn_text, "weather") == 0)
        {
            mode = 3;
            prompt_msgbox("天气信息获取中...");
        }
        else
            prompt_msgbox("已经连接服务器，请勿重复连接");
    }
    else // 未连接服务器
    {
        if (strcmp(btn_text, "connect") == 0)
        {
            // 启动客户端线程
            pthread_t thread;
            if (pthread_create(&thread, NULL, client, NULL) != 0)
            {
                perror("pthread_create ...");
            }
            // 设置分离
            pthread_detach(thread);
        }
        else if (strcmp(btn_text, "weather") == 0)
        {
            prompt_msgbox("未连接服务器，请先连接");
        }
    }
}

// 提示框显示
void prompt_msgbox(const char *info)
{
    LV_FONT_DECLARE(lv_font_HeiTi_Light_16_cjk);
    // 创建一个提示框对象
    lv_obj_t *alert_msgbox = lv_msgbox_create(lv_scr_act(), "提示", info, NULL, true);
    if (alert_msgbox == NULL)
    {
        perror("创建提示框失败");
        return;
    }
    lv_obj_set_style_text_font(alert_msgbox, &lv_font_HeiTi_Light_16_cjk, 0);

    // 获取关闭按钮并设置样式
    lv_obj_t *close_btn = lv_msgbox_get_close_btn(alert_msgbox);
    if (close_btn)
    {
        lv_obj_set_style_text_font(close_btn, &lv_font_simsun_16_cjk, LV_PART_MAIN);
        lv_obj_set_style_text_color(close_btn, lv_color_white(), LV_PART_MAIN);
    }
    else
    {
        perror("获取关闭按钮失败");
    }
    lv_obj_align(alert_msgbox, LV_ALIGN_CENTER, 0, 0);
}

// 返回(退出)按钮
void break_btn(lv_obj_t *main_windows)
{

    lv_obj_t *btn_obj = lv_btn_create(main_windows);

    // 设置按钮大小
    lv_obj_set_size(btn_obj, 73, 40);
    // 设置圆角
    lv_obj_set_style_radius(btn_obj, 0, LV_PART_MAIN);

    // 设置背景颜色
    lv_obj_set_style_bg_color(btn_obj, lv_color_hex(0x696969), LV_PART_MAIN);

    // 设置按钮位置
    lv_obj_set_pos(btn_obj, 2, 0);

    lv_obj_add_event_cb(btn_obj, break_btn_cb, LV_EVENT_CLICKED, "BREAK");

    /*  设置按钮的标签对象  */
    lv_obj_t *label_obj = lv_label_create(btn_obj);
    lv_label_set_text(label_obj, LV_SYMBOL_CLOSE);
    lv_obj_set_style_text_color(label_obj, lv_color_hex(0xFF0000), LV_PART_MAIN);
    lv_obj_center(label_obj);

    return;
}

// 事件处理回调函数
void break_btn_cb(lv_event_t *e)
{
    // 获取事件的用户数据
    const char *btn_text = (const char *)lv_event_get_user_data(e);
    printf("按下 %s\n", btn_text);
    if (strcmp(btn_text, "BREAK") == 0)
    {
        printf("正在释放资源\n");
        exit_demo2();
    }
}

// 界面标题标签
void title_label(lv_obj_t *main_windows)
{
    LV_FONT_DECLARE(lv_font_HeiTi_Bold_18_cjk); // 引入字体

    lv_obj_t *label = lv_label_create(main_windows);

    lv_label_set_text(
        label,
        "网络聊天室");
    lv_obj_set_style_text_font(label, &lv_font_HeiTi_Bold_18_cjk, 0);
    lv_obj_set_style_border_color(label, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_obj_set_pos(label, 430, 20);
}

// 显示退出界面
void exit_demo2()
{
    mode = 5; // 客户端退出模式标识
    lv_obj_t *main_windows = lv_obj_create(NULL);

    lv_obj_t *img = lv_img_create(main_windows);
    lv_img_set_src(img, "S:/mdata/jpg/1.png");

    lv_scr_load(main_windows);

    exit_flag = true;// 程序退出标志

    return;
}



// 登录界面(未完成)
void login_interface(lv_obj_t *main_windows)
{
    LV_FONT_DECLARE(lv_font_HeiTi_Light_16_cjk);

    lv_obj_t *login_windows = lv_obj_create(main_windows);

    lv_obj_set_size(login_windows, 541, 419);                // 设置画布大小
    lv_obj_set_pos(login_windows, 259, 59);                  // 设置画布位置
    lv_obj_set_style_radius(login_windows, 0, LV_PART_MAIN); // 圆角

    lv_obj_t *login_label = lv_label_create(login_windows);
    lv_label_set_text(login_label, "登录界面");
    lv_obj_set_style_text_font(login_label, &lv_font_HeiTi_Light_16_cjk, 0);
    lv_obj_set_style_border_color(login_label, lv_color_hex(0x000000), LV_PART_MAIN);
    lv_obj_align(login_label, LV_ALIGN_TOP_MID, 0, 0);

    // 账号输入框
    lv_obj_t *account_ta = lv_textarea_create(login_windows);
    lv_textarea_set_one_line(account_ta, true);
    lv_textarea_set_password_mode(account_ta, false);
    lv_textarea_set_one_line(account_ta, true);      
    lv_obj_set_width(account_ta, lv_pct(40));
    lv_obj_add_event_cb(account_ta, login_ta_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(account_ta, LV_ALIGN_TOP_LEFT, 5, 55);

    lv_obj_t *account_label = lv_label_create(login_windows);
    lv_label_set_text(account_label, "账号:");
    lv_obj_set_style_text_font(account_label, &lv_font_HeiTi_Light_16_cjk, 0);
    lv_obj_align_to(account_label, account_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    // 密码输入框
    pwd_ta = lv_textarea_create(login_windows);
    lv_textarea_set_text(pwd_ta, "");            // 清空文本框
    lv_textarea_set_password_mode(pwd_ta, true); // 密码模式
    lv_textarea_set_one_line(pwd_ta, true);      // 单行
    lv_obj_set_width(pwd_ta, lv_pct(40));        // 宽度
    lv_obj_align(pwd_ta, LV_ALIGN_TOP_RIGHT, -5, 55);
    lv_obj_add_event_cb(pwd_ta, login_ta_cb, LV_EVENT_ALL, NULL);

    lv_obj_t *pwd_label = lv_label_create(login_windows);
    lv_label_set_text(pwd_label, "密码:");
    lv_obj_set_style_text_font(pwd_label, &lv_font_HeiTi_Light_16_cjk, 0);
    lv_obj_align_to(pwd_label, pwd_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    // 键盘
    login_kb = lv_keyboard_create(login_windows);
    lv_obj_set_size(login_kb, lv_pct(100), 200);
    // 设置键盘与文本输入框关联
    lv_keyboard_set_textarea(login_kb, pwd_ta);
}
